package com.hw.pusher_example;

import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.hw.pusher.HWPushActivity;
import com.hw.pusher.utils.Logger;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends HWPushActivity {
//  @Override
//  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
//    GeneratedPluginRegistrant.registerWith(flutterEngine);
//
//  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(getFlutterEngine());
    getIntentUri();

    handleIntent();
  }

  @Override
  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    handleIntent();
  }

  void handleIntent() {
    Intent intent = getIntent();
    if (null != intent) {
      Bundle extras = intent.getExtras();
      if (null != extras) {

      }
    }
  }

  void getIntentUri() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
// Scheme协议（pushscheme://com.huawei.codelabpush/deeplink?）需要开发者自定义
    intent.setData(Uri.parse("push_scheme://com.hw.pusher_example/deeplink?"));
// 往intent中添加参数，用户可以根据自己的需求进行添加参数
    intent.putExtra("name", "abc");
//        intent.putExtra("age", 180);
// 必须带上该Flag
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addCategory(Intent.CATEGORY_BROWSABLE);
    String intentUri = intent.toUri(Intent.URI_INTENT_SCHEME);
// 打印出的intentUri值就是设置到推送消息中intent字段的值
    Log.d("intentUri", intentUri);
//        push_scheme://com.hw.pusher_example/deeplink?#Intent;launchFlags=0x4000000;S.name=abc;end
//        push_scheme://com.hw.pusher_example/deeplink?#Intent;action=android.intent.action.MAIN;launchFlags=0x4000000;S.name=abc;end
  }

  void g() {
    Intent intent = getIntent();
    if (null != intent) {
      // 方法1设置的数据通过如下方式获取
      String name1 = intent.getData().getQueryParameter("name");
//            int age1 = Integer.parseInt(intent.getData().getQueryParameter("age"));
      // 方法2设置的数据通过如下方式获取
      String name2 = intent.getStringExtra("name");
//            int age2 = intent.getIntExtra("age", -1);
      Logger.d( "==========================name " + name1 + ",age " );
    }
  }

}

